package com.web;

import com.domain.actPlan;
import com.service.userService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;


/**
 * 员工添加客户员工活动成绩
 * 接收addActPlan.jsp页面数据，封装为actPlan对象
 * 提交到userService的addActPlan函数
 * 重定向到/ShowUserAct
 */
@WebServlet("/addActPlan")
public class addActPlanServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.解决乱码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        //2.接收参数
        Map<String, String[]> map = request.getParameterMap();
        //3.封装成对象
        actPlan actP = new actPlan();
        try {
            BeanUtils.populate(actP,map);
        } catch (Exception e) {
            System.out.println(e);
        }
        System.out.println("添加了用户编号为 " + actP.getUserId()+"的用户数据");
        System.out.println("数据为" + actP.toString());
        /*//进行添加到表actPlan
        User userId = new userService().findUserId(actP.getUserId());//查询是否存在这个用户
        //判断添加的这个用户Id存不存在
        */
        //4.进行添加
        Integer add = new userService().addActPlan(actP);
        System.out.println("添加成功" + add );

        //5.进行重定向到查询用户参加活动的表
        response.sendRedirect(request.getContextPath()+"/ShowUserAct?userId="+actP.getUserId());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
